package com.xfc.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xfc.common.annotation.Log;
import com.xfc.common.enums.BusinessType;
import com.xfc.common.utils.DateUtils;
import com.xfc.config.Result;
import com.xfc.config.Tools;
import com.xfc.config.aspect.Token;
import com.xfc.message.NoticeMessage;
import com.xfc.school.domain.*;
import com.xfc.school.domain.vo.CalendarCourseVO;
import com.xfc.school.domain.vo.CourserListVO;
import com.xfc.school.domain.vo.ScStudentCourseVO;
import com.xfc.school.service.*;
import com.xfc.wx.domain.ScWxUser;
import com.xfc.wx.service.IScWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.*;

/**
 *  我的接口
 * @author lcj
 */
@Slf4j
@RestController
@Api(tags = "我的接口")
@Validated
@RequestMapping("/api")
public class WxMyController {

    @Autowired
    private IScWxUserService wxUserService;

    @Autowired
    private IScStudentService studentService;

    @Autowired
    private IScTeacherService teacherService;

    @Autowired
    private IScBuyCourseService buyCourseService;

    @Autowired
    private IScCourseService courseService;

    @Autowired
    private IScCourseListService courseListService;

    @Autowired
    private IScClassEvaluationFormService evaluationFormService;

    @Autowired
    private IScCourseScheduleService courseScheduleService;

    @Autowired
    private IScBuyCourseService scBuyCourseService;

    @Autowired
    private IScMarketingCourseService scMarketingCourseService;

    @Autowired
    private IScClassEvaluationFormService scClassEvaluationFormService;

    @Autowired
    private NoticeMessage message;

    /**
     *  查询当前用户绑定的学生信息
     * @return 返回用户学生列表
     */
    @Token
    @GetMapping("/getUserStudent")
    @ApiOperation(value = "查询当前用户绑定的学生信息")
    public Result<?> getUserStudent(ScWxUser wxUser){

        QueryWrapper<ScStudent> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_mobile",wxUser.getMobile()).or().eq("student_mobile2",wxUser.getMobile());
        return Result.ok(studentService.list(queryWrapper));
    }

    /**
     *  根据学生id查询学生购买课程
     * @return 返回用户学生购买课程
     */
    @GetMapping("/getCourseByStudentId")
    @ApiOperation(value = "根据学生id查询学生购买课程")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentId", value =  "学生id",  dataType = "String",required = true),
    })
    public Result<?> getCourseByStudentId(@NotBlank @RequestParam String studentId){
        //查询用户信息
        QueryWrapper<ScBuyCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        queryWrapper.orderByDesc("create_time");
        List<ScBuyCourse> list=buyCourseService.list(queryWrapper);
        list.forEach(l-> l.setCourseName(courseService.getById(l.getClassId()).getClassName()));
        return Result.ok(list);
    }



    /**
     *  根据学生id查询学生评价
     * @return 根据学生id查询学生评价
     */
    @GetMapping("/getEvaluateById")
    @ApiOperation(value = "根据学生id查询学生评价")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentId", value =  "学生id",  dataType = "String",required = true),
    })
    public Result<?> getEvaluateById(Page page,@NotBlank @RequestParam String studentId){
        QueryWrapper<ScClassEvaluationForm> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_number",studentId);
        queryWrapper.orderByDesc("create_time");
        //查询评价列表
        IPage<ScClassEvaluationForm> pageList=evaluationFormService.page(page,queryWrapper);
        pageList.getRecords().forEach(l->{
            l.setCourseName(Objects.isNull(courseService.getById(l.getCourseNumber()))?"":courseService.getById(l.getCourseNumber()).getClassName());
            l.setEvaluator(Objects.isNull(teacherService.getById(l.getEvaluator()))?"":teacherService.getById(l.getEvaluator()).getTeacherName());
            l.setStudentNumber(Objects.isNull(studentService.getById(l.getStudentNumber()))?"":studentService.getById(l.getStudentNumber()).getStudentName());
        });
        return Result.ok(pageList);
    }


    /**
     *  查询我的课程 (已进行/已结束)
     * @return 查询我的课程 (已进行/已结束)
     */
    @Token
    @GetMapping("/getConductCorse")
    @ApiOperation(value = "查询我的课程 (已进行/已结束)")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "current", value =  "当前页",  dataType = "int",required = true),
            @ApiImplicitParam( name = "size", value =  "每页显示条数",  dataType = "int",required = true),
            @ApiImplicitParam( name = "type", value =  "1-已进行2-已结束",  dataType = "int",required = true),
    })
    public Result<?> getConductCorse(ScWxUser wxUser, Page page,@RequestParam(value = "type",defaultValue = "1") Integer type){

        //查询学生信息
        List<ScStudent> studentList=studentService.list(new QueryWrapper<ScStudent>().eq("student_mobile",wxUser.getMobile()).or().eq("student_mobile2",wxUser.getMobile()));
        //查询我购买的课程
        if (Objects.isNull(studentList)){
            return Result.error("没有查询到学生信息");
        }
        List<String> studentIdList=new ArrayList<>();
        studentList.forEach(l->{
            studentIdList.add(l.getId());
        });
        IPage<ScStudentCourseVO> courseVOS=studentService.getStudentCourse(page,studentIdList,type);
        return Result.ok(courseVOS);
    }

    /**
     *  查询购买课程详情
     * @return
     */
    @GetMapping("/getBuyCourseById")
    @ApiOperation(value = "查询购买课程详情")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "buyId", value =  "购买课程id",  dataType = "String",required = true),
    })
    public Result<?> getBuyCourseById(@NotBlank @RequestParam String buyId){
        ScBuyCourse course=buyCourseService.getById(buyId);
        if (Objects.nonNull(course)){
            ScCourse course1=courseService.getById(course.getClassId());
            if (Objects.nonNull(course1)){
                course.setCourseName(course1.getClassName());
            }
            ScStudent student=studentService.getById(course.getStudentId());
            if (Objects.nonNull(student)){
                course.setStudentId(student.getStudentName());
            }
        }
        return Result.ok(course);
    }

    /**
     * 我的课程(日历/老师)
     * @return
     */
    @Token
    @GetMapping("/getCalendarCorseByTeacher")
    @ApiOperation(value = "我的课程(日历/老师)")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "time", value =  "时间(年-月)",  dataType = "String",required = true),
    })
    public Result<?> getCalendarCorse(ScWxUser wxUser,@NotBlank @RequestParam String time){
        //转化时间
        Date date = DateUtil.parse(time+"-01");
        //补差多少天
        int day= DateUtils.getMonthWeek(date);
        //获取起始日期
        Date startTime = DateUtil.offset(date, DateField.DAY_OF_MONTH, day);
        Date endTime = DateUtils.lastMonthDay(date);

        //获取天数
        List<String> dateList=DateUtils.getMonthDay(startTime,endTime);
        Collections.reverse(dateList);
        //获取老师id
        ScTeacher teacher=teacherService.getOne(new QueryWrapper<ScTeacher>().eq("teacher_mobile",wxUser.getMobile()));
        if (Objects.isNull(teacher)){
            return Result.error("没有老师信息");
        }
        //查询老师课程
        List<ScCourseList> courserList=courseListService.list(new QueryWrapper<ScCourseList>().eq("teacher_id",teacher.getId()));
        Set<String> courserIdList=new HashSet<>();
        courserList.forEach(l->{
            courserIdList.add(l.getClassId());
        });
        //存储课程
        List<CalendarCourseVO> calendarList= new ArrayList<>();
        //查询是否有天数
        dateList.forEach(l->{
            CalendarCourseVO courseVO=new CalendarCourseVO();
            int count=0;
            if (courserIdList.size()>0){
                QueryWrapper<ScCourseSchedule> queryWrapper1=new QueryWrapper<>();
                queryWrapper1.in("course_number",courserIdList);
                queryWrapper1.ne("create_time",1);
                queryWrapper1.apply(StringUtils.isNotBlank(l),
                        "date_format (start_time,'%Y-%m-%d') = date_format('" + l + "','%Y-%m-%d')");
                count=courseScheduleService.count(queryWrapper1);
            }
            //是否有课
            courseVO.setDot(count>0?true:false);
            String[] str=l.split("-");
            courseVO.setDay(str[2]);
            courseVO.setMonth(str[1]);
            calendarList.add(courseVO);
        });
        return Result.ok(calendarList);
    }

    /**
     * 课程(日历)
     * @return
     */
    @GetMapping("/getCalendarCorse")
    @ApiOperation(value = "课程(日历)")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentId", value =  "学生id",  dataType = "String",required = true),
            @ApiImplicitParam( name = "time", value =  "时间(年-月)",  dataType = "String",required = true),
    })
    public Result<?> getCalendarCorse(@NotBlank @RequestParam String studentId,@NotBlank @RequestParam String time){
        //转化时间
        Date date = DateUtil.parse(time+"-01");
        //补差多少天
        int day= DateUtils.getMonthWeek(date);
        //获取起始日期
        Date startTime = DateUtil.offset(date, DateField.DAY_OF_MONTH, day);
        Date endTime = DateUtils.lastMonthDay(date);

        //获取天数
        List<String> dateList=DateUtils.getMonthDay(startTime,endTime);
        Collections.reverse(dateList);
        //查询学生是否有课程
        QueryWrapper<ScBuyCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        queryWrapper.ge("class_surplus_time",0);
        queryWrapper.apply(StringUtils.isNotBlank(time),
                "date_format (start_time,'%Y-%m') <= '" + time+"'" );
        queryWrapper.apply(StringUtils.isNotBlank(time),
                "date_format (end_time,'%Y-%m') >= '" + time +"'");
        List<ScBuyCourse> studentCourse=buyCourseService.list(queryWrapper);
        Set<String> courserIdList=new HashSet<>();
        studentCourse.forEach(l->{
            courserIdList.add(l.getClassId());
        });
        //存储课程
        List<CalendarCourseVO> calendarList= new ArrayList<>();
        //查询是否有天数
        dateList.forEach(l->{
            CalendarCourseVO courseVO=new CalendarCourseVO();
            int count=0;
            if (courserIdList.size()>0){
                QueryWrapper<ScCourseSchedule> queryWrapper1=new QueryWrapper<>();
                queryWrapper1.in("course_number",courserIdList);
                queryWrapper1.ne("create_time",1);
                queryWrapper1.apply(StringUtils.isNotBlank(l),
                        "date_format (start_time,'%Y-%m-%d') = date_format('" + l + "','%Y-%m-%d')");
                count=courseScheduleService.count(queryWrapper1);
            }
            //是否有课
            courseVO.setDot(count>0?true:false);
            String[] str=l.split("-");
            courseVO.setDay(str[2]);
            courseVO.setMonth(str[1]);
            calendarList.add(courseVO);
        });
        return Result.ok(calendarList);
    }

    /**
     * 课程列表(日历/老师)
     * @return
     */
    @Token
    @GetMapping("/getCorseListByteacher")
    @ApiOperation(value = "课程列表(日历/老师)")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "time", value =  "时间(年-月-日)",  dataType = "String",required = true),
    })
    public Result<?> getCorseListByteacher(ScWxUser wxUser,@NotBlank @RequestParam String time){
        //获取老师id
        ScTeacher teacher=teacherService.getOne(new QueryWrapper<ScTeacher>().eq("teacher_mobile",wxUser.getMobile()));
        if (Objects.isNull(teacher)){
            return Result.error("没有老师信息");
        }
        //查询老师课程
        List<ScCourseList> courserList=courseListService.list(new QueryWrapper<ScCourseList>().eq("teacher_id",teacher.getId()));
        Set<String> courserIdList=new HashSet<>();
        courserList.forEach(l->{
            courserIdList.add(l.getClassId());
        });
        //查询课程列表
        QueryWrapper<ScCourseSchedule> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.in("course_number",courserIdList);
        queryWrapper1.apply(StringUtils.isNotBlank(time),
                "date_format (start_time,'%Y-%m-%d') = date_format('" + time + "','%Y-%m-%d')");
        queryWrapper1.ne("course_state",1);
        List<ScCourseSchedule> res=courseScheduleService.list(queryWrapper1);
        res.forEach(l->{
            l.setCourseName(Objects.isNull(courseService.getById(l.getCourseNumber()))?"":courseService.getById(l.getCourseNumber()).getClassName());
        });
        return Result.ok(res);
    }

    /**
     * 课程列表(日历)
     * @return
     */
    @GetMapping("/getCalendarCorseList")
    @ApiOperation(value = "课程列表(日历)")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentId", value =  "学生id",  dataType = "String",required = true),
            @ApiImplicitParam( name = "time", value =  "时间(年-月-日)",  dataType = "String",required = true),
    })
    public Result<?> getCalendarCorseList(@NotBlank @RequestParam String studentId,@NotBlank @RequestParam String time){
        //查询学生是否有课程
        QueryWrapper<ScBuyCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        queryWrapper.ge("class_surplus_time",0);
        List<ScBuyCourse> studentCourse=buyCourseService.list(queryWrapper);
        Set<String> courserIdList=new HashSet<>();
        studentCourse.forEach(l->{
            courserIdList.add(l.getClassId());
        });
        //查询课程列表
        QueryWrapper<ScCourseSchedule> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.in("course_number",courserIdList);
        queryWrapper1.apply(StringUtils.isNotBlank(time),
                "date_format (start_time,'%Y-%m-%d') = date_format('" + time + "','%Y-%m-%d')");
        queryWrapper1.ne("course_state",1);
        List<ScCourseSchedule> res=courseScheduleService.list(queryWrapper1);
        res.forEach(l->{
            l.setCourseName(Objects.isNull(courseService.getById(l.getCourseNumber()))?"":courseService.getById(l.getCourseNumber()).getClassName());
        });
        return Result.ok(res);
    }


    /**
     * 首页列表(我的课程)
     * @return
     */
    @Token
    @GetMapping("/getHomeList")
    @ApiOperation(value = "首页列表(我的课程)")
    public Result<?> getHomeList(ScWxUser wxUser){
        //获取时间段
        String startTime=DateUtil.today();
        //结束时间
        String endTime=DateUtil.formatDate(DateUtil.offset( DateUtil.date(), DateField.DAY_OF_MONTH, 7));
        //查询绑定学生
        List<ScStudent> studentCourse=studentService.list(new QueryWrapper<ScStudent>().eq("student_mobile",wxUser.getMobile()).or().eq("student_mobile2",wxUser.getMobile()));
        List<String> studentIdList=new ArrayList<>();
        studentCourse.forEach(l->{
            studentIdList.add(l.getId());
        });
        //查询学生的课程
        List<CourserListVO> res=studentService.getHomeCourseList( studentIdList,startTime,endTime);
        return Result.ok(res);
    }

    /**
     * 首页列表(我的课程全部)
     * @return
     */
    @Token
    @GetMapping("/getHomeAllList")
    @ApiOperation(value = "首页列表(我的课程全部)")
    public Result<?> getHomeAllList(ScWxUser wxUser){
        //查询绑定学生
        List<ScStudent> studentCourse=studentService.list(new QueryWrapper<ScStudent>().eq("student_mobile",wxUser.getMobile()).or().eq("student_mobile2",wxUser.getMobile()));
        List<String> studentIdList=new ArrayList<>();
        studentCourse.forEach(l->{
            studentIdList.add(l.getId());
        });
        //查询学生的课程
        List<CourserListVO> res=studentService.getHomeCourseList(studentIdList,null,null);
        return Result.ok(res);
    }

    /**
     * 首页列表(查询所有课程)
     * @return
     */
    @GetMapping("/getAllCourse")
    @ApiOperation(value = "首页列表(查询所有课程)")
    public Result<?> getAllCourse(){
        QueryWrapper<ScCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("state",0);
        return Result.ok(courseService.list(queryWrapper));
    }


    /**
     * 我的主页
     * @return
     */
    @Token
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "我的主页")
    public Result<?> getUserInfo(ScWxUser wxUser){

        return Result.ok(wxUserService.getById(wxUser.getId()));
    }

    /**
     * 老师消课评价列表
     * @return
     */
    @Token
    @GetMapping("/getCancelClass")
    @ApiOperation(value = "老师消课评价列表")
    public Result<?> getCancelClass(ScWxUser wxUser, String startTime, String courseNumber,Page page){
        //获取老师id
        ScTeacher teacher=teacherService.getOne(new QueryWrapper<ScTeacher>().eq("teacher_mobile",wxUser.getMobile()));
        if (Objects.isNull(teacher)){
            return Result.error("没有老师信息");
        }
        QueryWrapper<ScCourseSchedule> queryWrapper=new QueryWrapper<>();
        queryWrapper.apply(Objects.nonNull(startTime),
                "date_format (start_time,'%Y-%m-%d') = date_format('" +startTime+ "','%Y-%m-%d')");
        if(Objects.nonNull(courseNumber)){
            queryWrapper.eq("course_number",courseNumber);
        }
        queryWrapper.eq("teacher_number",teacher.getId());
        queryWrapper.le("start_time",new Date());
        queryWrapper.orderByDesc("start_time");
        IPage<ScCourseSchedule> pageList = courseScheduleService.page(page,queryWrapper);
        //查询课程信息
        pageList.getRecords().forEach(l->{
            //查询课程名称
            if (Objects.nonNull(l.getCourseNumber())) {
                ScCourse scClass = courseService.getById(l.getCourseNumber());
                if (Objects.nonNull(scClass)) {
                    //课程名称
                    l.setCourseName(scClass.getClassName());
                }
            }
            l.setTeacherName(teacher.getTeacherName());
        });
        return Result.ok(pageList);
    }


    /**
     *  消课上课人数
     * @param classId 课程id
     * @param courseStartime 课程开始时间
     * @param counrseEndtime 课程结束时间
     * @return Result
     */
    @Token
    @ApiOperation(value = "消课上课人数")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "classId", value =  "课程id",  dataType = "String",required = true),
            @ApiImplicitParam( name = "courseStartime", value =  "上课开始时间",  dataType = "String",required = true),
            @ApiImplicitParam( name = "counrseEndtime", value =  "上课结束时间",  dataType = "String",required = true)
    })
    @GetMapping("/list/schedule")
    public Result<?> listSchedule(@NotBlank @RequestParam String classId,@NotBlank @RequestParam String courseStartime ,
                                      @NotBlank @RequestParam String counrseEndtime ) {

        QueryWrapper<ScBuyCourse> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.apply(
                "date_format (start_time,'%Y-%m-%d') <= date_format('" +courseStartime+ "','%Y-%m-%d')");
        queryWrapper1.apply(
                "date_format (end_time,'%Y-%m-%d') >= date_format('" +counrseEndtime+ "','%Y-%m-%d')");
        queryWrapper1.eq("class_id",classId);
        queryWrapper1.orderByAsc("create_time");
        List<ScBuyCourse> list = scBuyCourseService.list(queryWrapper1);
        list.forEach(l->{
            //学生姓名
            ScStudent student=studentService.getById(l.getStudentId());
            if (Objects.nonNull(student)){
                l.setStudentName(student.getStudentName());
                l.setStudentSex(String.valueOf(student.getStudentSex()));
                l.setStudentAge(String.valueOf(Tools.getAgeByBirth(student.getStudentBirthday())));
            }
            //查询是否销课
            QueryWrapper<ScMarketingCourse> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("student_id",l.getStudentId());
            queryWrapper.eq("course_id",l.getClassId());
            queryWrapper.apply(
                    "date_format (course_startime,'%Y-%m-%d %h-%m') = date_format('" +courseStartime+ "','%Y-%m-%d %h-%m')");
            queryWrapper.select("state");
            List<ScMarketingCourse> list2 = scMarketingCourseService.list(queryWrapper);
            list2.forEach(l2-> l.setMarketingState(l2.getState()));

            //查询课堂评价的id
            QueryWrapper<ScClassEvaluationForm> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.apply(
                    "date_format (start_time,'%Y-%m-%d %h-%m') = date_format('" +courseStartime+ "','%Y-%m-%d %h-%m')");
            queryWrapper3.eq("course_number",classId);
            queryWrapper3.eq("student_number",l.getStudentId());
            List<ScClassEvaluationForm> listEvaluation = scClassEvaluationFormService.list(queryWrapper3);
            listEvaluation.forEach(l3-> l.setEvaluationId(l3.getId()));
        });
        return Result.ok(list);
    }


    /**
     *  老师消课
     * @return Result
     */
    @Token
    @ApiOperation(value = "老师消课")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "ScMarketingCourse", value =  "消课记录",  dataType = "String",required = true),
            @ApiImplicitParam( name = "ScBuyCourse", value =  "购买课程实体类",  dataType = "String",required = true)
    })
    @PostMapping("/addTeacherCancelClass")
    public Result<?> addTeacherCancelClass(ScWxUser wxUser,@RequestBody ScMarketingCourse scMarketingCourse,ScBuyCourse scBuyCourse) {
        scMarketingCourse.setCreateTime(new Date());
        Integer surplusTiem=scMarketingCourse.getClassSurplusTime();
        if(Objects.nonNull(surplusTiem)){
            scBuyCourse.setClassSurplusTime(surplusTiem-1);
        }
        scBuyCourse.setId(scMarketingCourse.getByclassid());
        scBuyCourseService.updateById(scBuyCourse);
        //查询课程是否已消课
        QueryWrapper<ScMarketingCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_id",scMarketingCourse.getStudentId());
        queryWrapper.eq("course_id",scMarketingCourse.getByclassid());
        queryWrapper.apply(
                "date_format (course_startime,'%Y-%m-%d %h-%m') = date_format('" +scBuyCourse.getCourseStartime()+ "','%Y-%m-%d %h-%m')");

        ScMarketingCourse scMarketingCourse1=scMarketingCourseService.getOne(queryWrapper);
        if (Objects.nonNull(scMarketingCourse1)){
            scMarketingCourse.setId(scMarketingCourse1.getId());
        }
        QueryWrapper<ScTeacher> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("teacher_mobile",wxUser.getMobile());
        queryWrapper1.eq("state",0);
        //查询老师是否存在
        ScTeacher scTeacher=teacherService.getOne(queryWrapper1);
        scMarketingCourse.setTeacherId(scTeacher.getId());
        scMarketingCourseService.saveOrUpdate(scMarketingCourse);
        //查询课程
        ScCourse course=courseService.getById(scMarketingCourse.getCourseId());
        if (Objects.nonNull(course)){
            //查询学生信息
            ScStudent student=studentService.getById(scMarketingCourse.getStudentId());
            if (Objects.nonNull(student)){
                //查询openid
                ScWxUser wxUser1=wxUserService.getOne(new QueryWrapper<ScWxUser>().eq("mobile",student.getStudentMobile()));
                if (Objects.nonNull(wxUser1)) {
                    message.courseDisappear(wxUser1.getOpenId(), student.getStudentName(), scMarketingCourse.getCourseStartime(), course.getClassName(), "1", String.valueOf(scBuyCourse.getClassSurplusTime()));
                }
            }else{
                log.error("没有查询到学生，微信通知失败");
            }
        }else {
           log.error("没有查询到课程，微信通知失败");
        }

        return Result.ok("操作成功");
    }

    /**
     * 获取课堂评价详细信息
     */
    @ApiOperation(value = "获取课堂评价详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "pjId", value =  "评价id",  dataType = "String",required = true),
    })
    @GetMapping("/getEvaluateInfo")
    public Result<?> getEvaluateInfo(@NotBlank @RequestParam String pjId) {

        return Result.ok(scClassEvaluationFormService.getById(pjId));
    }

    /**
     * 课堂评价
     */
    @Token
    @ApiOperation(value = "课堂评价")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "ScClassEvaluationForm", value =  "scClassEvaluationForm",  dataType = "String",required = true),
    })
    @Log(title = "课堂评价", businessType = BusinessType.UPDATE)
    @PostMapping("/evaluate")
    public Result<?> evaluate(ScWxUser wxUser,@RequestBody ScClassEvaluationForm scClassEvaluationForm) {

        if (Objects.isNull(scClassEvaluationForm)){
            return Result.error("没有评价信息");
        }
        QueryWrapper<ScTeacher> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("teacher_mobile",wxUser.getMobile());
        queryWrapper1.eq("state",0);
        //查询老师是否存在
        ScTeacher scTeacher=teacherService.getOne(queryWrapper1);
        //创建时间
        scClassEvaluationForm.setCreateTime(new Date());
        //添加老师id
        scClassEvaluationForm.setEvaluator(scTeacher.getId());
        //更新评价
        scClassEvaluationFormService.saveOrUpdate(scClassEvaluationForm);
        //查询课程是否已消课
        QueryWrapper<ScMarketingCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student_id",scClassEvaluationForm.getStudentNumber());
        queryWrapper.eq("course_id",scClassEvaluationForm.getCourseNumber());
        queryWrapper.eq("course_startime",scClassEvaluationForm.getStartTime());
        queryWrapper.eq("counrse_endtime",scClassEvaluationForm.getEndTime());
//        queryWrapper.apply(
//                "date_format (course_startime,'%Y-%m-%d %h-%m') = date_format('" +scClassEvaluationForm.getStartTime()+ "','%Y-%m-%d %h-%m')");

        ScMarketingCourse scMarketingCourse1=scMarketingCourseService.getOne(queryWrapper);
        if (Objects.nonNull(scMarketingCourse1)){
            log.info("课程已消");
        }else {
            ScMarketingCourse scMarketingCourse=new ScMarketingCourse();
            scMarketingCourse.setCourseId(scClassEvaluationForm.getCourseNumber());
            scMarketingCourse.setCreateTime(new Date());
            scMarketingCourse.setState("0");
            scMarketingCourse.setStudentId(scClassEvaluationForm.getStudentNumber());
            scMarketingCourse.setCourseStartime(scClassEvaluationForm.getStartTime());
            scMarketingCourse.setCounrseEndtime(scClassEvaluationForm.getEndTime());
            //查询课程id
            ScCourseList scCourse=courseListService.getOne(new QueryWrapper<ScCourseList>().eq("class_id",scClassEvaluationForm.getCourseNumber()));
            if (Objects.nonNull(scCourse)) {
                scMarketingCourse.setTeacherId(scCourse.getTeacherId());
            }
            scMarketingCourseService.save(scMarketingCourse);
            //发送通知
            QueryWrapper<ScBuyCourse> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("class_id",scClassEvaluationForm.getCourseNumber());
            queryWrapper2.eq("student_id",scClassEvaluationForm.getStudentNumber());
            queryWrapper2.gt("class_surplus_time",0);
            queryWrapper2.orderByDesc("create_time");
            queryWrapper2.last("limit 1");
            ScBuyCourse scBuyCourse=scBuyCourseService.getOne(queryWrapper2);
            if (Objects.nonNull(scBuyCourse)) {
                scBuyCourse.setClassSurplusTime(scBuyCourse.getClassSurplusTime()-1);
                scBuyCourse.setUpdateTime(new Date());
                scBuyCourseService.updateById(scBuyCourse);
                //查询课程名称
                ScCourse course = courseService.getById(scClassEvaluationForm.getCourseNumber());
                if (Objects.nonNull(course)) {
                    //查询学生信息
                    ScStudent student = studentService.getById(scClassEvaluationForm.getStudentNumber());
                    if (Objects.nonNull(course)) {
                        //查询用户openId
                        ScWxUser wxUser1 = wxUserService.getOne(new QueryWrapper<ScWxUser>().eq("mobile", student.getStudentMobile()));
                        if (Objects.nonNull(wxUser1)) {
                            //发送通知
                            message.courseDisappear(wxUser1.getOpenId(), student.getStudentName(), scClassEvaluationForm.getStartTime(), course.getClassName(), "1", String.valueOf(scBuyCourse.getClassSurplusTime()));
                        }
                    } else {
                        log.error("没有查询到用户课程,通知失败");
                    }
                } else {
                    log.error("没有查询到用户openId,通知失败");
                }
            }else {
                log.error("没有查询到购买课程,通知失败");
            }
        }
        //学生评价
        //评价通知
        ScCourse course = courseService.getById(scClassEvaluationForm.getCourseNumber());
        if (Objects.nonNull(course)) {
            //查询学生信息
            ScStudent student = studentService.getById(scClassEvaluationForm.getStudentNumber());
            if (Objects.nonNull(course)) {
                //查询用户openId
                ScWxUser wxUser1 = wxUserService.getOne(new QueryWrapper<ScWxUser>().eq("mobile", student.getStudentMobile()));
                //查询老师信息
                if (Objects.nonNull(wxUser1)&&Objects.nonNull(scTeacher)) {
                    message.courseEvaluate(wxUser1.getOpenId(),student.getStudentName(),course.getClassName(),scTeacher.getTeacherName());
                }
            } else {
                log.error("没有查询到用户课程,通知失败");
            }
        } else {
            log.error("没有查询到用户openId,通知失败");
        }
        return Result.ok("评价成功");
    }
}
